package rpcserver

import (
	rpcv1 "gluenet/internal/agent/api/rpc/v1"
	"gluenet/internal/agent/depend"
	"gluenet/internal/agent/driver/rpcserver"
	"gluenet/internal/agent/service/maassvc"
	"gluenet/utils/rpc/v2/define"
)

const (
	maas = rpcv1.RPC_SERVICE_MAAS
)

func init() {
	depend.Provide(NewMaasRouter)
	registerRouter(maas, func(c rpcserver.IRoutes) {
		depend.Invoke(func(d *maasRouter) {
			d.initRouter(c)
		})
	})
}

type maasRouter struct {
	maas *maassvc.Service
}

func NewMaasRouter(svc *maassvc.Service) *maasRouter {
	return &maasRouter{svc}
}

func (r *maasRouter) initRouter(c rpcserver.IRoutes) {
	c.Group("maas").
		Func("boot_resources_search", r.bootResourcesSearch).
		Func("subnets_search", r.subNetsSearch).
		Func("vm_host_search", r.vmHostSearch).
		Func("vm_hosts_search", r.vmHostsSearch).
		Func("vm_hosts_create", r.vmHostsCreate).
		Func("vm_hosts_delete", r.vmHostsDelete).
		Func("machine_create", r.machinesCreate).
		Func("machine_delete", r.machinesDelete).
		Func("machine_update", r.machinesUpdate).
		Func("machine_search", r.machineSearch).
		Func("machine_detail", r.machineDetail).
		Func("machines_search", r.machinesSearch).
		Func("machine_power_on", r.machinesPowerOn).
		Func("machine_power_off", r.machinesPowerOff).
		Func("tags_update_nodes", r.tagsUpdateNodes).
		Func("machine_deploy", r.machineDeploy).
		Func("subnets_ip_addresses", r.subNetsIPAddressesSearch)
}

func (r *maasRouter) bootResourcesSearch(c *rpcserver.Context) {
	maasReq := new(define.MaasReq)
	if err := c.BindJSON(&struct {
		Spec interface{} `json:"spec,omitempty"`
	}{maasReq}); err != nil {
		return
	}
	resp := r.maas.BootResourcesSearch(*maasReq)
	c.JSON(resp)
}

func (r *maasRouter) subNetsSearch(c *rpcserver.Context) {
	maasReq := new(define.MaasReq)
	if err := c.BindJSON(&struct {
		Spec interface{} `json:"spec,omitempty"`
	}{maasReq}); err != nil {
		return
	}
	resp := r.maas.SubNetsSearch(*maasReq)
	c.JSON(resp)
}

func (r *maasRouter) vmHostSearch(c *rpcserver.Context) {
	maasReq := new(define.MaasReq)
	if err := c.BindJSON(&struct {
		Spec interface{} `json:"spec,omitempty"`
	}{maasReq}); err != nil {
		return
	}
	resp := r.maas.VMHostSearch(*maasReq)
	c.JSON(resp)
}

func (r *maasRouter) vmHostsSearch(c *rpcserver.Context) {
	maasReq := new(define.MaasReq)
	if err := c.BindJSON(&struct {
		Spec interface{} `json:"spec,omitempty"`
	}{maasReq}); err != nil {
		return
	}
	resp := r.maas.VMHostsSearch(*maasReq)
	c.JSON(resp)
}

func (r *maasRouter) vmHostsCreate(c *rpcserver.Context) {
	maasReq := new(define.MaasReq)
	if err := c.BindJSON(&struct {
		Spec interface{} `json:"spec,omitempty"`
	}{maasReq}); err != nil {
		return
	}
	resp := r.maas.VMHostsCreate(*maasReq)
	c.JSON(resp)
}

func (r *maasRouter) vmHostsDelete(c *rpcserver.Context) {
	maasReq := new(define.MaasReq)
	if err := c.BindJSON(&struct {
		Spec interface{} `json:"spec,omitempty"`
	}{maasReq}); err != nil {
		return
	}
	resp := r.maas.VMHostsDelete(*maasReq)
	c.JSON(resp)
}

func (r *maasRouter) machinesCreate(c *rpcserver.Context) {
	maasReq := new(define.MaasReq)
	if err := c.BindJSON(&struct {
		Spec interface{} `json:"spec,omitempty"`
	}{maasReq}); err != nil {
		return
	}
	resp := r.maas.MachinesCreate(*maasReq)
	c.JSON(resp)
}

func (r *maasRouter) machinesDelete(c *rpcserver.Context) {
	maasReq := new(define.MaasReq)
	if err := c.BindJSON(&struct {
		Spec interface{} `json:"spec,omitempty"`
	}{maasReq}); err != nil {
		return
	}
	resp := r.maas.MachinesDelete(*maasReq)
	c.JSON(resp)
}

func (r *maasRouter) machinesUpdate(c *rpcserver.Context) {
	maasReq := new(define.MaasReq)
	if err := c.BindJSON(&struct {
		Spec interface{} `json:"spec,omitempty"`
	}{maasReq}); err != nil {
		return
	}
	resp := r.maas.MachinesUpdate(*maasReq)
	c.JSON(resp)
}

func (r *maasRouter) machineSearch(c *rpcserver.Context) {
	maasReq := new(define.MaasReq)
	if err := c.BindJSON(&struct {
		Spec interface{} `json:"spec,omitempty"`
	}{maasReq}); err != nil {
		return
	}
	resp := r.maas.MachineSearch(*maasReq)
	c.JSON(resp)
}

func (r *maasRouter) machineDetail(c *rpcserver.Context) {
	maasReq := new(define.MaasReq)
	if err := c.BindJSON(&struct {
		Spec interface{} `json:"spec,omitempty"`
	}{maasReq}); err != nil {
		return
	}
	resp := r.maas.MachineDetail(*maasReq)
	c.JSON(resp)
}

func (r *maasRouter) machinesSearch(c *rpcserver.Context) {
	maasReq := new(define.MaasReq)
	if err := c.BindJSON(&struct {
		Spec interface{} `json:"spec,omitempty"`
	}{maasReq}); err != nil {
		return
	}
	resp := r.maas.MachinesSearch(*maasReq)
	c.JSON(resp)
}

func (r *maasRouter) machinesPowerOn(c *rpcserver.Context) {
	maasReq := new(define.MaasReq)
	if err := c.BindJSON(&struct {
		Spec interface{} `json:"spec,omitempty"`
	}{maasReq}); err != nil {
		return
	}
	resp := r.maas.MachinesPowerOn(*maasReq)
	c.JSON(resp)
}

func (r *maasRouter) machinesPowerOff(c *rpcserver.Context) {
	maasReq := new(define.MaasReq)
	if err := c.BindJSON(&struct {
		Spec interface{} `json:"spec,omitempty"`
	}{maasReq}); err != nil {
		return
	}
	resp := r.maas.MachinesPowerOff(*maasReq)
	c.JSON(resp)
}

func (r *maasRouter) tagsUpdateNodes(c *rpcserver.Context) {
	maasReq := new(define.MaasReq)
	if err := c.BindJSON(&struct {
		Spec interface{} `json:"spec,omitempty"`
	}{maasReq}); err != nil {
		return
	}
	resp := r.maas.TagsUpdateNodes(*maasReq)
	c.JSON(resp)
}

func (r *maasRouter) machineDeploy(c *rpcserver.Context) {
	maasReq := new(define.MaasReq)
	if err := c.BindJSON(&struct {
		Spec interface{} `json:"spec,omitempty"`
	}{maasReq}); err != nil {
		return
	}
	resp := r.maas.MachineDeploy(*maasReq)
	c.JSON(resp)
}

func (r *maasRouter) subNetsIPAddressesSearch(c *rpcserver.Context) {
	maasReq := new(define.MaasReq)
	if err := c.BindJSON(&struct {
		Spec interface{} `json:"spec,omitempty"`
	}{maasReq}); err != nil {
		return
	}
	resp := r.maas.SubNetsIPAddressesSearch(*maasReq)
	c.JSON(resp)
}
